const Model = require('../dbs/model/getCollection.js')
const ProcessHistorySchema = require('../dbs/schema/processHistorySchema.js')
const result = require('../com/result.js')

/**
 * 正在进行或者已经完成的流程数据service - 多半是查
 * @param {*} ctx 
 * @param {*} next 
 */
let getHistory = async (ctx, next) => {
    let param = ctx.query;
    if (!param.instanceId) {
        ctx.body = result.go(result.code.CODE_501);
        return;
    }
    // if (!param.schoolId || !param.pageNum || !param.size) {
    //     ctx.body = result.go(result.code.CODE_501);
    //     return;
    // }

    // let pageNum = Number(param.pageNum) - 1;
    // let size = Number(param.size);

    // let where = {
    //     schoolId: param.schoolId
    // };

    // if (param.id) {
    //     where['_id'] = param.id;
    // }
    // if (param.instanceYes == 'false') {
    //     where.instanceYes = false;
    // } else if (param.instanceYes == 'true') {
    //     where.instanceYes = true;
    // }
    // if (param.sosSite) {
    //     where.sosSite = param.sosSite;
    // }
    // if (param.studentId) {
    //     where.studentId = param.studentId;
    // }
    // if (param.instanceId) {
    //     where.instanceId = param.instanceId;
    // }
    // if (param.peopleId) {
    //     where.peopleId = param.peopleId;
    // }
    // if (param.processType) {
    //     where.processType = {
    //         $in: JSON.parse(param.processType)
    //     };
    // }
    // if (param.startTime) {
    //     //数据库的时间大于等于这个时间
    //     let i = String(param.startTime).length > 10 ? 1 : 1000;
    //     let timeNum = parseInt(param.startTime, 10) * i;
    //     where.createTime = {
    //         $gte: new Date(timeNum)
    //     };
    // }
    // if (param.endTime) {
    //     //数据库的时间小于这个时间
    //     let i = String(param.endTime).length > 10 ? 1 : 1000;
    //     let timeNum = parseInt(param.endTime, 10) * i;
    //     let endTime = new Date(timeNum);
    //     if (where.createTime && where.createTime['$gte']) {
    //         where.createTime['$lt'] = endTime;
    //     } else {
    //         where.createTime = {
    //             $lt: endTime
    //         };
    //     }
    // }

    let ProcessHistoryModel = await Model.getComModel({
        dataBase: 'com_info',
        tableName: 'process_history',
        schema: ProcessHistorySchema
    });

    return await new Promise((res, rej) => {
        // let i = 0;
        // let data = {};
        // ProcessHistoryModel.countDocuments(where, (error, count) => {
        //     i++;
        //     data.allCount = count;
        //     if (i == 2) {
        //         ctx.body = result.go(result.code.CODE_200, data);
        //         return res();
        //     }
        // });
        ProcessHistoryModel
            .findOne({
                instanceId: param.instanceId
            })
            // .skip(pageNum * size)
            // .limit(size)
            // .sort({ createTime: -1 })
            .exec((error, doc) => {
                // i++;
                // data.data = doc;
                // if (i == 2) {
                ctx.body = result.go(result.code.CODE_200, doc);
                return res();
                // }
            });
    });

};

/**
 * 查某些流程进行或者已经完成的数据-只是最简单的外层数据-已办任务
 * @param {*} ctx 
 * @param {*} next 
 */
let getHistoryList = async (ctx, next) => {
    let param = ctx.query;
    // if (!param.schoolId || !param.pageNum || !param.size) {
    //     ctx.body = result.go(result.code.CODE_501);
    //     return;
    // }

    let pageNum = 0, size = 10, peopleId = param.peopleId;
    if (param.pageNum) {
        pageNum = Number(param.pageNum) - 1;
    }
    if (param.size) {
        size = Number(param.size);
    }

    let where = {
        peopleId: peopleId
    };

    if (param.startTime) {
        //数据库的时间大于等于这个时间
        let i = String(param.startTime).length > 10 ? 1 : 1000;
        let timeNum = parseInt(param.startTime, 10) * i;
        where.createTime = {
            $gte: new Date(timeNum)
        };
    }
    if (param.endTime) {
        //数据库的时间小于这个时间
        let i = String(param.endTime).length > 10 ? 1 : 1000;
        let timeNum = parseInt(param.endTime, 10) * i;
        let endTime = new Date(timeNum);
        if (where.createTime && where.createTime['$gte']) {
            where.createTime['$lt'] = endTime;
        } else {
            where.createTime = {
                $lt: endTime
            };
        }
    }

    let ProcessHistoryModel = await Model.getComModel({
        dataBase: 'com_info',
        tableName: 'process_history',
        schema: ProcessHistorySchema
    });

    return await new Promise((res, rej) => {
        ProcessHistoryModel
            .find(where)
            .skip(pageNum * size)
            .limit(size)
            .sort({ createTime: -1 })
            .exec((error, doc) => {
                ctx.body = result.go(result.code.CODE_200, handleTaskHis(doc, peopleId));
                return res();
            });
    });

};

let handleTaskHis = (doc, peopleId) => {
    let returnArr = [];
    for (let i in doc) {
        let one = doc[i];
        //保安到达
        let reach = one.instanceInfo.reach;
        //保安处理
        let overAlarm = one.instanceInfo.overAlarm;
        //保安队长已分配任务
        let leaderComplate = one.instanceInfo.leaderComplate;
        if ((reach&& reach.figure.peopleId == peopleId) || overAlarm || leaderComplate) {

        }
        if (reach && reach.figure.peopleId == peopleId) {
            returnArr.push({
                instanceYes: one.instanceYes,
                sosSite: one.sosSite,
                student: one.sosSite,
                instanceId: one.instanceId,
                taskName: '已到达报警点',
                chartType: 'SOS校徽报警',
                instanceCreateTime: one.createTime,
                instanceOverTime: one.overTime,
                taskOverTime: reach.time
            });
        }
        if (overAlarm && overAlarm.figure.peopleId == peopleId) {
            returnArr.push({
                instanceYes: one.instanceYes,
                sosSite: one.sosSite,
                student: one.sosSite,
                instanceId: one.instanceId,
                taskName: '已处理SOS校徽报警',
                chartType: 'SOS校徽报警',
                instanceCreateTime: one.createTime,
                instanceOverTime: one.overTime,
                taskOverTime: overAlarm.time
            });
        }
        if (leaderComplate && leaderComplate.whoLeader.peopleId == peopleId) {
            returnArr.push({
                instanceYes: one.instanceYes,
                sosSite: one.sosSite,
                student: one.sosSite,
                instanceId: one.instanceId,
                taskName: '已分配任务给保安',
                chartType: 'SOS校徽报警',
                instanceCreateTime: one.createTime,
                instanceOverTime: one.overTime,
                taskOverTime: leaderComplate.time
            });
        }
    }
    return returnArr;
};

module.exports = {
    getHistory,
    // getHistoryList
}  